import { Component, ComponentUnitType } from "@/stores/edit-pages-store";
import { without } from "lodash-es";

// 判断编辑行是否有禁编辑字段

let preOperateInvalidField = "";
export function checkLineWithoutArrField(str: string, disabledFields: Array<string>): boolean {
  const preStr = str.split(":")?.[0];
  if (!preStr) {
    return true;
  }
  const editStr = preStr.trim().replace(/"/g, "");

  //处理禁止修改字段恢复后onchange触发问题
  {
    if (preOperateInvalidField === editStr) {
      setTimeout(() => {
        preOperateInvalidField = "";
      }, 1300);
      return true;
    }
    preOperateInvalidField = editStr;
  }

  return disabledFields.includes(editStr) ? false : true;
}

function objectHasRequiredKeys(obj: Record<string, any>, requiredKeys: Array<string>): boolean {
  return requiredKeys.every(key => obj.hasOwnProperty(key));
}
export function checkComponentsAllValid(component: Component, disabledFields: Array<string>): boolean {
  const disArr = component.name === "Page" ? without(disabledFields, "parentId") : disabledFields;
  const state = objectHasRequiredKeys(component, disArr);
  if (!state) {
    return false;
  } else {
    const { body, aside, footer, header, buttonGroup, definitions } = component;
    const arr = [...body||[], ...aside||[], ...footer||[], ...header||[], ... buttonGroup||[], ...definitions||[]]
    if (Array.isArray(body) && body.length > 0) {
      return body.every(item => checkComponentsAllValid(item, disabledFields));
    } else {
      return true;
    }
  }
}
